Component({
  mixins: [],
  data: {
    shop: {},
    name: '有股购-分享有礼',
    start: '2020-04-01',
    end: '2020-04-30',
    instruction: '1.本活动严禁作弊|2.如对本活动有疑问，可以联系商铺电话',
    phone: '01086466630',
    isFavor: ''
  },
  props: {},
  didMount() {
    this.loadShop();
    this.loadData();
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onTimeChange(e) {
      let time = e.detail.value;
      this.setData({
        start: time[0],
        end: time[1]
      })
    },
    onChange(e) {
      let {
        detail: { value },
        currentTarget: { dataset }
      } = e;
      let { name } = dataset;
      this.setData({ [name]: value });
    },
    goGood() {
      my.navigateTo({
        url: '/pages/good/good'
      });
    },
    goCoupon() {
      my.navigateTo({
        url: '/pages/coupon/coupon'
      });
    },
    goImage() {
      my.navigateTo({
        url: '/pages/image/image'
      });
    },
    async loadShop() {
      const { cloud } = getApp();
      try {
        const res = await cloud.topApi.invoke({
          api: 'taobao.shop.seller.get',
          authScope: '*',
          data: { fields: 'sid,title,pic_path' }
        });
        this.setData({
          shop: res.shop
        })
      } catch (e) {
        my.alert({ content: 'error ' + e.message })
      }
    },
    async loadData() {
      const { cloud } = getApp();
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/getset',
        method: 'GET',
        params: { shop_id: getApp().globalData.shop_id }
      })

      if (res) {
        this.setData({
          name: res.name,
          start: res.start,
          end: res.end,
          instruction: res.instruction,
          phone: res.phone,
          isFavor: res.isFavor,
          goodList: res.good,
          coupon: res.coupon,
          topImage:res.topImage
        })
      }
    },
    async handleSubmit() {
      if (this.data.name == '') {
        my.alert({
          content: '活动名称未设置'
        })
        return false;
      }
      if (this.data.start == '') {
        my.alert({
          content: '活动日期未设置'
        })
        return false;
      }
      if (this.data.end == '') {
        my.alert({
          content: '活动日期未设置'
        })
        return false;
      }
      if (this.data.instruction == '') {
        my.alert({
          content: '活动说明未设置'
        })
        return false;
      }

      if (this.data.phone == '') {
        my.alert({
          content: '客服电话未设置'
        })
        return false;
      }

      let sendData = {
        shop_id: this.data.shop.sid,
        shop_logo: 'http://logo.taobao.com/shop-logo' + this.data.shop.pic_path,
        shop_title: this.data.shop.title,
        name: this.data.name,
        start: this.data.start,
        end: this.data.end,
        instruction: this.data.instruction,
        phone: this.data.phone,
        isFavor: this.data.isFavor

      };
      const { cloud } = getApp();
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/writeset',
        method: 'POST',
        params: sendData
      });
      my.alert({
        title: '温馨提示',
        content: '修改成功,请在手机端查看效果！'
      })


    }
  },
});
